package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.entity.PurchaserChargeEntity;
import com.antler.smt.request.admin.ConditionSeeKBaseReq;
import com.antler.smt.response.admin.GetPurchaserVipInfoListResp;
import com.antler.smt.response.admin.GetSupplierListResp;
import com.antler.smt.response.admin.OrderManageListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.ConditionSeekService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 条件查询实体
 *
 * @author zhangyi
 * @date 2018/5/10 11:06
 */
@RestController
@RequestMapping(value = "/conditionSeek")
@Api(description = "条件查询实体")
public class ConditionSeekController extends BaseController{
    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    /**
     * 手机号验证正则表达式
     */
    private static Pattern phone = Pattern.compile("^((17[0-9])|(13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");

    /**
     * 邮箱验证正则表达式
     */
    private static Pattern email = Pattern.compile("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$");

    @Resource
    private ConditionSeekService conditionSeekService;

    /**
     * 分类查询服务商列表
     *
     * @param conditionSeeKBaseReq 请求分类查询实体
     * @return Result实体
     */
    @GetMapping("/getSupplierSeekInfo")
    @ApiOperation(value = "条件查询用户列表（传入状态信息（用“,”号分割））", httpMethod = "GET")
    @OperateCut(content = "分类查询服务商列表")
    public Result<PageInfo<GetSupplierListResp>> getSupplierInfo(ConditionSeeKBaseReq conditionSeeKBaseReq) {

        DataValidator.isNull(conditionSeeKBaseReq,"分页条件不能为空！");

        Result result = null;
            if(conditionSeeKBaseReq.getStatusString() != null){
                List<Integer> integers = splitList(conditionSeeKBaseReq.getStatusString());
                conditionSeeKBaseReq.setStatus(integers);
            }else if(conditionSeeKBaseReq.getSettedStatusString() != null){
                conditionSeeKBaseReq.setSettedStatus(splitList(conditionSeeKBaseReq.getSettedStatusString()));
            }
            String account = conditionSeeKBaseReq.getAccount();

            if(account != null) {
                judgementAccount(account,conditionSeeKBaseReq);
            }
            result = conditionSeekService.getSupplierInfo(conditionSeeKBaseReq);
        return result;
    }

    /**
     * 分类查询采购商列表
     *
     * @param conditionSeeKBaseReq 请求分类查询实体
     * @param status 用户状态信息列表
     * @return Result实体
     */
    @GetMapping("/getPurchaserSeekInfo")
    @ApiOperation(value = "条件查询用户列表（传入状态信息（用“,”号分割））", httpMethod = "GET")
    @OperateCut(content = "分类查询采购商列表")
    public Result<PageInfo<GetPurchaserVipInfoListResp>> getPurchaserInfo(ConditionSeeKBaseReq conditionSeeKBaseReq,
                                                                          @RequestParam(value = "status") String status) {
        DataValidator.isNull(conditionSeeKBaseReq,"分页条件不能为空！");
        DataValidator.isBlank(status,"采购商分类状态不能为空！");

        Result result = null;
            String account = conditionSeeKBaseReq.getAccount();
            conditionSeeKBaseReq.setStatus(splitList(status));
            if(account != null) {
                judgementAccount(account,conditionSeeKBaseReq);
            }
            result = conditionSeekService.getPurchaserInfo(conditionSeeKBaseReq);
        return result;
    }


    /**
     * 分类获取订单列表
     *
     * @param conditionSeeKBaseReq 请求分类查询实体
     * @param status 用户状态信息列表
     * @return Result实体
     */
    @GetMapping("/getOrderSeekInfo")
    @ApiOperation(value = "条件查询订单列表（传入状态信息（用“,”号分割））", httpMethod = "GET")
    @OperateCut(content = "分类查询订单列表")
    public Result<PageInfo<OrderManageListResp>> getOrderSeekInfo(ConditionSeeKBaseReq conditionSeeKBaseReq,
                                                                  @RequestParam(value = "status") String status) {
        DataValidator.isNull(conditionSeeKBaseReq,"分页条件不能为空！");
        DataValidator.isBlank(status,"订单分类状态不能为空！");

        Result result = null;
            conditionSeeKBaseReq.setStatus(splitList(status));
            result = conditionSeekService.getOrderInfo(conditionSeeKBaseReq,getUserId());
        return result;
    }

    /**
     * 分类获取采购商充值待审核列表
     *
     * @param conditionSeeKBaseReq 请求分类查询实体
     * @return Result实体
     */
    @GetMapping("/purchaserCharge")
    @ApiOperation(value = "分类获取采购商充值列表", httpMethod = "GET")
    @OperateCut(content = "分类查询采购商充值列表")
    public Result<PageInfo<PurchaserChargeEntity>> purchaserCharge(ConditionSeeKBaseReq conditionSeeKBaseReq) {

        DataValidator.isNull(conditionSeeKBaseReq,"分页实体不能为空！");

        Result result = null;
            String account = conditionSeeKBaseReq.getAccount();
            if(account != null) {
                judgementAccount(account,conditionSeeKBaseReq);
            }
            result = conditionSeekService.getPurchaserWaitChargeList(conditionSeeKBaseReq);
        return result;
    }

    public void  judgementAccount(String account,ConditionSeeKBaseReq conditionSeeKBaseReq){
        //判断是否是手机号
        Matcher pm = phone.matcher(account);
        //判断是否是邮箱
        Matcher em = email.matcher(account);
        boolean isPhone = pm.matches();
        boolean isEmail = em.matches();
        if(isPhone){
            conditionSeeKBaseReq.setMobile(account);
        }else if(isEmail){
            conditionSeeKBaseReq.setEmail(account);
        }else{
            conditionSeeKBaseReq.setMobile(account);
            conditionSeeKBaseReq.setEmail(account);
        }
    }

    public List<Integer> splitList(String status){
        String[] split = status.split(",");
        List<Integer> list = new ArrayList<>();
        for(String s : split){
            list.add(Integer.parseInt(s));
        }
        return list;
    }
}
